package com.practice.service.admin.impl;

import com.practice.common.context.BaseContext;
import com.practice.common.result.Result;
import com.practice.mapper.admin.AdminIndexMapper;
import com.practice.pojo.admin.entity.*;
import com.practice.pojo.admin.vo.ProfileVO;
import com.practice.pojo.admin.vo.BaseInfoVO;
import com.practice.service.admin.AdminIndexService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminIndexServiceImpl implements AdminIndexService {

    @Autowired
    private AdminIndexMapper adminIndexMapper;

    @Override
    public Result config() {
        //调用mapper获取系统数据
        List<String> STR = adminIndexMapper.config();

        Map<String,String> map = new HashMap();
        if (STR.get(0) != null){
            map.put("login_bg",STR.get(0));
        }
        if (STR.get(1) != null){
            map.put("login_bg",STR.get(1));
        }
        if (STR.get(1) != null){
            map.put("system_name",STR.get(2));
        }
        if (STR.get(1) != null){
            map.put("system_logo",STR.get(3));
        }
        if (STR.get(1) != null){
            map.put("web_ico",STR.get(4));
        }


        return Result.success(map);
    }

    @Override
    public ProfileVO profile() {
        //用户资料 sys_user
        UserInfo userInfo = new UserInfo();

        Long userId = BaseContext.getCurrentId();
        //查询所有用户信息
        User user = adminIndexMapper.selectUser(userId);

        BeanUtils.copyProperties(user,userInfo);
        //根据用户id获取roleid
        String role_id = adminIndexMapper.getByRole(BaseContext.getCurrentId());

        //切割处理
        String roleId = role_id.substring(2, role_id.length() - 2);

        //根据roleid获取管理员权限
//        String rolename = adminIndexMapper.getByRoleName(roleId);
        String rolename = adminIndexMapper.getByRoleName(roleId);

        userInfo.setCreate_date(user.getCreateDate());
        userInfo.setLogin_date(user.getLoginDate());
        userInfo.setLogin_ip(user.getLoginIp());
        userInfo.setLogin_name(user.getLoginName());
        userInfo.setDel_flag(user.getDelFlag());
        userInfo.setUpdate_date(user.getUpdateDate());
        userInfo.setRole_id(role_id);
        userInfo.setRolename(rolename);


        //菜单权限 sys _menu
            //根据role_id查询menu_id
        List<MenuList> menuLists= adminIndexMapper.selectMenu(roleId);
//        List<MenuList> menuLists= adminIndexMapper.selectMenu();

        //dictsList sys_dict
        //interface_type 接口类型
        List<Dict> interface_type = adminIndexMapper.getByinterface(LoginConstant.INTERFACE_TYPE);
        //order_type    交易类型
        List<Dict> order_type = adminIndexMapper.getByOrder(LoginConstant.ORDER_TYPE);
        //report_type  报告类型
        List<Dict> report_type = adminIndexMapper.getByReport(LoginConstant.REPORT_TYPE);
        //table_type    表类型
        List<Dict> table_type = adminIndexMapper.getByTable(LoginConstant.TABLE_TYPE);
        //theme     主题类型
        List<Dict> theme = adminIndexMapper.getByTheme(LoginConstant.THEME);

        //整合类型,存入集合
        Map<String,String> interfaceList = new HashMap<>();
        for (Dict dict : interface_type) {
            interfaceList.put(dict.getValue(),dict.getLabel());
        }

        Map<String,String> orderList = new HashMap<>();
        for (Dict dict : order_type) {
            orderList.put(dict.getValue(),dict.getLabel());
        }

        Map<String,String> reportList = new HashMap<>();
        for (Dict dict : report_type) {
            reportList.put(dict.getValue(),dict.getLabel());
        }

        Map<String,String> tableList = new HashMap<>();
        for (Dict dict : table_type) {
            tableList.put(dict.getValue(),dict.getLabel());
        }

        Map<String,String> themeList = new HashMap<>();
        for (Dict dict : theme) {
            themeList.put(dict.getValue(),dict.getLabel());
        }

//        List<Map<String,String>> dictsList = new ArrayList();
//        dictsList.add(interfaceList);
//        dictsList.add(orderList);
//        dictsList.add(reportList);
//        dictsList.add(tableList);
//        dictsList.add(themeList);
        Map<String,Map<String,String>> map = new HashMap<>();
        map.put("interface_type",interfaceList);
        map.put("order_type", orderList);
        map.put("report_type", reportList);
        map.put("table_type",tableList);
        map.put("theme",themeList);


        //btnList
        List<String> btnList = new ArrayList<>();
        btnList.add("*");

        ProfileVO vo = ProfileVO.builder()
                .userInfo(userInfo)
                .menuList(menuLists)
                .dictsList(map)
                .btnList(btnList)
                .build();

        return vo;
    }

    @Override
    public BaseInfoVO baseInfo() {

        //获取分页类型
        List<AdminCate> all_cate = adminIndexMapper.getByCate();

        //获取最新用户数据
        List<AdminYddUser> new_user = adminIndexMapper.getByYddUser();

        //获取热门文章数据
        List<AdminHotArticle> hot_article = adminIndexMapper.getByHotArticle();

        //获取用户数据
        Integer total = adminIndexMapper.getByTotal();
        Integer user_man = adminIndexMapper.getByMan(LoginConstant.MAN);
        Integer user_woman = adminIndexMapper.getByWomen(LoginConstant.WOMAN);
        Map<String,Integer> userMap = new HashMap();
        userMap.put("total",total);
        userMap.put("user_man",user_man);
        userMap.put("user_woman",user_woman);

        //广告位总数
        Integer advposNum = adminIndexMapper.getByAdvposNum();

        //标签总数
        Integer tagNum = adminIndexMapper.getByTag();

        //管理员总数
        Integer adminNum = adminIndexMapper.getByAdminNum();

        //文章总数
        Integer articleNum = adminIndexMapper.getByArticleNum();

        //分类总数
        Integer cate_num = adminIndexMapper.getByCateNum();

        //广告图总数
        Integer advimgNum = adminIndexMapper.getByAdvimgNum();

        BaseInfoVO vo = BaseInfoVO.builder()
                .allCate(all_cate)
                .new_user(new_user)
                .hot_article(hot_article)
                .user(userMap)
                .advposNum(advposNum)
                .tagNum(tagNum)
                .adminNum(adminNum)
                .articleNum(articleNum)
                .cate_num(cate_num)
                .advimgNum(advimgNum)
                .build();

        return vo;
    }
}
